package com.wltt.product.ahuthr.service;

import com.wltt.product.ahuthr.config.exception.BusinessException;
import com.wltt.product.ahuthr.mapper.SystemAgreementAttributeMapper;
import com.wltt.product.ahuthr.model.QueryAgreementAttributeRsp;
import com.wltt.product.ahuthr.model.ResultData;
import com.wltt.product.ahuthr.model.po.SystemAgreementAttributePO;
import com.wltt.product.ahuthr.model.vo.QueryAgreementAttributeReq;
import com.wltt.product.ahuthr.service.base.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;


/**
 * @ClassName: SystemAgreementAttributeServiceImpl
 * @Description: 系统协议类型属性 逻辑服务类<br/>
 * 不做业务校验，参数校验统一放到前置的Service中
 * @Date: 2024-08-07 09:50:27
 * @Author yanggl
 */
@Service(value = "systemAgreementAttributeService")
public class SystemAgreementAttributeServiceImpl extends BaseServiceImpl<SystemAgreementAttributePO, Long> implements SystemAgreementAttributeService {

    private SystemAgreementAttributeMapper systemAgreementAttributeMapper;

    //构造方法
    public SystemAgreementAttributeServiceImpl(SystemAgreementAttributeMapper systemAgreementAttributeMapper) {
        super(systemAgreementAttributeMapper);
        this.systemAgreementAttributeMapper = systemAgreementAttributeMapper;
    }

    /**
     * 查询合同属性
     *
     * @param queryAgreementAttributeReq
     * @return
     */
    @Override
    public ResultData queryAgreementAttribute(QueryAgreementAttributeReq queryAgreementAttributeReq) {

        if (null == queryAgreementAttributeReq.getAgreementTypeId()) {
            throw new BusinessException("未指定合同类别", HttpStatus.BAD_REQUEST);
        }

        List<SystemAgreementAttributePO> poList = systemAgreementAttributeMapper.queryByTypeId(queryAgreementAttributeReq.getAgreementTypeId());
        List<QueryAgreementAttributeRsp> rspList = Optional.ofNullable(poList).orElse(new ArrayList<>())
                .stream().map(po -> {
                    QueryAgreementAttributeRsp rsp = new QueryAgreementAttributeRsp();
                    BeanUtils.copyProperties(po, rsp);
                    return rsp;
                }).toList();

        return ResultData.success(rspList);
    }
}